package com.wayne.handlers;

import com.wayne.domain.User;
import com.wayne.repository.UserRepository;
import com.wayne.util.CheckUtil;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8;
import static org.springframework.http.MediaType.TEXT_EVENT_STREAM;
import static org.springframework.web.reactive.function.server.ServerResponse.notFound;
import static org.springframework.web.reactive.function.server.ServerResponse.ok;

@Component
public class UserHandler {
    private final UserRepository repository;

    public UserHandler(UserRepository repository) {
        this.repository = repository;
    }

    /**
     * 获取全部的用户
     *
     * @param request
     * @return
     */
    public Mono<ServerResponse> getAllUser(ServerRequest request) {
        return ok()
                .contentType(APPLICATION_JSON_UTF8)
                .body(repository.findAll(), User.class);
    }

    /**
     * 获取全部的用户
     *
     * @param request
     * @return
     */
    public Mono<ServerResponse> getStreamAllUser(ServerRequest request) {
        return ok()
                .contentType(TEXT_EVENT_STREAM)
                .body(repository.findAll(), User.class);
    }

    /**
     * 创建用户
     *
     * @param request
     * @return
     */
    public Mono<ServerResponse> createUser(ServerRequest request) {
        Mono<User> userMono = request.bodyToMono(User.class);
        return userMono.flatMap(user -> {
            user.setId(null);
            CheckUtil.checkName(user.getName());
            return ok()
                    .contentType(APPLICATION_JSON_UTF8)
                    .body(repository.save(user), User.class);
        });
    }

    /**
     * 刪除用户
     *
     * @param request
     * @return
     */
    public Mono<ServerResponse> deleteUser(ServerRequest request) {
        String id = request.pathVariable("id");
        return repository.findById(id)
                .flatMap(user -> repository.delete(user)
                        .then(ok().build()))
                .switchIfEmpty(notFound().build());
    }

    /**
     * 根據ID查找用户
     *
     * @param request
     * @return
     */
    public Mono<ServerResponse> findUserById(ServerRequest request) {
        String id = request.pathVariable("id");
        return repository.findById(id)
                .flatMap(user -> ok().body(user, User.class))
                .switchIfEmpty(notFound().build());
    }

    /**
     * 根據ID修改用户
     *
     * @param request
     * @return
     */
    public Mono<ServerResponse> updateUser(ServerRequest request) {
        String id = request.pathVariable("id");
        Mono<User> userMono = request.bodyToMono(User.class);
        return userMono.flatMap(user -> {
            return repository.findById(id)
                    .flatMap(u -> {
                        u.setName(user.getName());
                        u.setAge(user.getAge());
                        return ok().body(repository.save(u), User.class);
                    })
                    .switchIfEmpty(notFound().build());
        });
    }
}
